<template>
  <div class="container">
    <div
        style="height: 100vh;justify-content: center;color: white;background: rgb(16, 25, 56) url('/loginImg.jpg');
      background-size: 100% 100%;font-size: 26px;font-family: 宋体;display: flex">
    </div>
    <div class="login-container">
      <!--标题-->
      <div>
        <h1 class="title1">智慧消防监控管理平台</h1>
      </div>
      <!--表单-->
      <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-card" label-width="25%"
               @keyup.enter.native="submitForm('loginForm')">
        <!--欢迎使用-->
        <h1 class="title2">欢迎使用</h1>
        <!-- 账户-->
        <el-form-item class="name" label="" prop="username" style="width: 90%;padding-top: 8px;">
          <el-input style="color:#000;" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <!-- 密码-->
        <el-form-item class="password" label="" prop="password" style="width: 90%;padding-top: 4px">
          <el-input style="color:#000;" v-model="loginForm.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <!--验证码-->
        <el-form-item class="code" label="" prop="code" style="width: 90%">
          <el-input v-model="loginForm.code" class="code-input" placeholder="请输入验证码"
                    style="color:#000;width: 50%;float: left"></el-input>
          <span style="padding-left: 35px" @click="refreshCode">
          <s-identify :identifyCode="identifyCode"></s-identify>
          </span>
        </el-form-item>
        <!--登录按钮-->
        <el-form-item>
          <el-button :disabled="logining" style="width: 65%" type="primary" @click="submitForm('loginForm')"><h2>登
            录</h2></el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import SIdentify from "@/components/SIdentify.vue";
import request from "@/utils/request.js"
export default {
  name: "Login",
  components: {SIdentify},
  data() {
    return {
      // 表单信息
      loginForm: {
        username: '',
        password: '',
        code: '',
      },
      identifyCodes: '1234567890',//随机串内容
      identifyCode: '',
      logining:false,
      // 表单验证
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        code: [
          {required: true, message: '验证码不能为空', trigger: 'blur'},
        ]
      }
    }
  },
  // 默认验证码
  created() {
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  },
  methods: {
    // 刷新验证码
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },

    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length)]
      }
    },

    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    },
    redirect(){
      const redirect =  localStorage.getItem("redirectPath")
      console.log((redirect))
      if (redirect!=undefined) {
        return true
      }
      return false
    },
    submitForm: function (formName) {
      // 表单验证
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.loginForm.code.toLowerCase() !== this.identifyCode.toLowerCase()) {
            this.$message.error('请填写正确验证码')
            this.refreshCode()
            return
          }
          this.logining = true;
          // 发送请求(post示例)
          request.post("/user/login", this.loginForm).then(res => {
            if (res.code === "200") {
              localStorage.setItem("user", JSON.stringify(res.data))
              if (this.redirect()) {
                let fullPath = localStorage.getItem("redirectPath");
                fullPath = "addrCodeAbout" + fullPath.substring(fullPath.indexOf("?"))
                this.$router.push(fullPath)
                localStorage.removeItem("redirectPath")
              } else {
                this.$router.push("/")
              }
              this.$store.state.tabList = []
              this.$store
                      .dispatch("utils/getUserInfo", {initSocket: true})
                      .then((res) => {
                      });
            } else if (res.code === "402") {
              this.logining = false;
              this.$message.error('账户已锁定')
              return false;
            } else {
              this.logining = false;
              this.$message.error('用户名或密码错误')
              return false;
            }
          })
        } else {
          return false;
        }
      })
    }
  }
}

</script>

<style scoped>
:deep(.el-input__inner){
  color: black;
}
.container {
  width: 100%;
  height: 100%;
  text-align: center;
}
.login-container {
  display: inline-block;
  width: 600px;
  height: 400px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%); /* 50%为自身尺寸的一半 */
}

.title1 {
  width: 500px;
  text-shadow: -3px 3px 1px #5f565e;
  margin: auto;
  color: whitesmoke;
  font-size: 42px;
}

.title2 {
  color: rgb(0, 166, 255);
  font-size: 28px;
}

.login-card {
  width: 450px;
  margin: auto;
  background-color: rgb(255, 255, 255);
  border-radius: 20px;
  padding: 10px 30px 10px 10px;
  opacity: 0.9;
}

.name {
  background: url("../assets/name.png") no-repeat;
  background-size: 30px 30px;
  background-position: 17%;
}

.password {
  background: url("../assets/password.png") no-repeat;
  background-size: 30px 30px;
  background-position: 17%;
}

.code {
  background: url("../assets/code.png") no-repeat;
  background-size: 30px 30px;
  background-position: 17%;
}

</style>
